package com.alex.springbootdemo.service.impl;

import com.alex.springbootdemo.mapper.mbg.UmsResourceMapper;
import com.alex.springbootdemo.model.pojo.UmsResource;
import com.alex.springbootdemo.service.UmsResourceService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;

/**
 * 后台资源管理Service实现类
 */
@Service
public class UmsResourceServiceImpl implements UmsResourceService {
    @Autowired
    private UmsResourceMapper resourceMapper;

    @Override
    public List<UmsResource> getResourceListByAdminId(Long adminId) {
        return resourceMapper.getResourceList(adminId);
    }

    @Override
    public List<UmsResource> listAll() {
        return resourceMapper.selectAll();
    }

    @Override
    public int create(UmsResource umsResource) {
        umsResource.setCreateTime(new Date());
        return resourceMapper.insertSelective(umsResource);
    }

    @Override
    public int update(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        return resourceMapper.updateByPrimaryKeySelective(umsResource);
    }

    @Override
    public UmsResource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return resourceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UmsResource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        Weekend<UmsResource> weekend = Weekend.of(UmsResource.class);
        WeekendCriteria<UmsResource, Object> weekendCriteria = weekend.weekendCriteria();
        if(categoryId!=null){
            weekendCriteria.andEqualTo(UmsResource::getCategoryId,categoryId);
        }
        if(StringUtils.isNotEmpty(nameKeyword)){
            weekendCriteria.andLike(UmsResource::getName,"%"+nameKeyword+"%");
        }
        if(categoryId!=null){
            weekendCriteria.andLike(UmsResource::getUrl,"%"+urlKeyword+"%");
        }
        PageHelper.startPage(pageNum,pageSize);
        return resourceMapper.selectByExample(weekend);
    }
}
